<?php
require "Zend/Session/Namespace.php";
class BaseController extends Zend_Controller_Action
{
    private $req;
    public $user_id;
    public $account;
    public $pwd;
    public $is_login;

    function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
        Zend_Session::start(); //　开启session
        parent::__construct($request, $response, $invokeArgs);
        $req = $this->getRequest();
        if ($req->getControllerName() != 'login' ) {
            $this->authCheck();
        }
    }

    final function authCheck(){
        $user_session = new Zend_Session_Namespace('login');
        if ((isset($user_session->is_login) == false || $user_session->is_login == false)) {
            $this->_redirect('/login');
        } else {
            $this->user_id = $user_session->user_id;
            $this->account = $user_session->account;
            $this->pwd = $user_session->pwd;
            $this->is_login = $user_session->is_login;
        }
    }

    final function object2array($object) {
        $object =  json_decode( json_encode( $object),true);
        return  $object;
    }

    final function makeUrl($searchData=array()){
        $url = '';
        if(!empty($searchData)){
            foreach($searchData as $key=>$value) {
                $url .= "&$key=$value";
            }
            return  substr($url,1,strlen($url));
        }else{
            return '';
        }
    }

    public function send_template_message($data)
    {
        $access_token = $this->get_access_token();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $res = $this->http_request($url,$data);
        return json_decode($res,true);
    }

    private function get_access_token()
    {
        $app_id = \Util\Config::get('weixin_appid','Env');
        $app_secret = \Util\Config::get('weixin_appsecret','Env');
        $res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$app_id.'&secret='.$app_secret);
        $res = json_decode($res, true);
        $access_token = $res['access_token'];
        return $access_token;
    }

    private function http_request($url,$data = null)
    {
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);//需要获取的URL地址
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);//禁用后cURL将终止从服务端进行验证
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);//检查服务器SSL证书中是否存在一个公用名(common name)
        if (!empty($data)){
            curl_setopt($curl,CURLOPT_POST,1);//启用时会发送一个常规的POST请求
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);//	全部数据使用HTTP协议中的"POST"操作来发送
        }
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。 returntransfer
        $output = curl_exec($curl);
        if (curl_errno($curl))
        {
            return 'Errno' . curl_error($curl);
        }
        curl_close($curl);
        return $output;
    }
}